// This code is from CQU CS course embedded system 2022 spring LAB


// 1. Pre-processor Directives Section
// Constant declarations to access port registers using 
// symbolic names instead of addresses
#include "TExaS.h"
#define GPIO_PORTF_DATA_R       (*((volatile unsigned long *)0x400253FC))
#define GPIO_PORTF_DIR_R        (*((volatile unsigned long *)0x40025400))
#define GPIO_PORTF_AFSEL_R      (*((volatile unsigned long *)0x40025420))
#define GPIO_PORTF_PUR_R        (*((volatile unsigned long *)0x40025510))
#define GPIO_PORTF_DEN_R        (*((volatile unsigned long *)0x4002551C))
#define GPIO_PORTF_LOCK_R       (*((volatile unsigned long *)0x40025520))
#define GPIO_PORTF_CR_R         (*((volatile unsigned long *)0x40025524))
#define GPIO_PORTF_AMSEL_R      (*((volatile unsigned long *)0x40025528))
#define GPIO_PORTF_PCTL_R       (*((volatile unsigned long *)0x4002552C))
#define SYSCTL_RCGC2_R          (*((volatile unsigned long *)0x400FE108))

// 2. Declarations Section
//   Global Variables
unsigned long In;  // input from PF0
unsigned long Out; // outputs to PF3,PF2,PF1 (multicolor LED)

//   Function Prototypes
void PortF_Init(void);
void Delay(void);
void EnableInterrupts(void);



// =============== Begin for 1 led ===============
// 3. Subroutines Section
// MAIN: Mandatory for a C Program to be executable
// int main(void){    
// 	TExaS_Init(SW_PIN_PF40,LED_PIN_PF321); // this initializes the TExaS grader lab 2
//   PortF_Init();        // Call initialization of port PF4 PF2    
//   EnableInterrupts();  // The grader uses interrupts
//   while(1){
// 		In = GPIO_PORTF_DATA_R&0x01; // read PF0 into In
//     if(In == 0x00){              // zero means SW is pressed
//       GPIO_PORTF_DATA_R = 0x00;  // LED is green
// 		} else{                      // 0x10 means SW is not pressed
//       GPIO_PORTF_DATA_R = 0x02;  // LED is pink
//     }
//     // GPIO_PORTF_DATA_R = 0x02;
//     Delay();                     // wait 0.1 sec
//     GPIO_PORTF_DATA_R = 0x00;    // LED is sky blue
//     Delay();                     // wait 0.1 sec
//   }
// }


// Subroutine to initialize port F pins for input and output
// PF4 and PF0 are input SW1 and SW2 respectively
// PF3,PF2,PF1 are outputs to the LED
// Inputs: None
// Outputs: None
// Notes: These five pins are connected to hardware on the LaunchPad
// void PortF_Init(void){ volatile unsigned long delay;
//   SYSCTL_RCGC2_R |= 0x00000020;     // 1) F clock
//   delay = SYSCTL_RCGC2_R;           // delay   
//   GPIO_PORTF_LOCK_R = 0x4C4F434B;   // 2) unlock PortF PF0  
//   GPIO_PORTF_CR_R = 0x03;           // allow changes to PF1-0       011
//   GPIO_PORTF_AMSEL_R = 0x00;        // 3) disable analog function
//   GPIO_PORTF_PCTL_R = 0x00000000;   // 4) GPIO clear bit PCTL  
//   GPIO_PORTF_DIR_R = 0x02;          // 5) PF0 input, PF1 output   00000010
//   GPIO_PORTF_AFSEL_R = 0x00;        // 6) no alternate function
//   GPIO_PORTF_PUR_R = 0x01;          // enable pullup resistors on PF0    00000001
//   GPIO_PORTF_DEN_R = 0x03;          // 7) enable digital pins PF1-PF0    0000011    
// }
// =============== End for 1 led ===============


// ===============Begin for 3 leds ===============
int main(void){    
  TExaS_Init(SW_PIN_PF40,LED_PIN_PF321); 
  PortF_Init();        // Call initialization of port PF4 PF2    
  EnableInterrupts();  // The grader uses interrupts
  while(1){
    In = GPIO_PORTF_DATA_R&0x01; // read PF0 into In
    if(In == 0x00){              // zero means SW is pressed
      GPIO_PORTF_DATA_R = 0x08;  // LED is green
		}
    else{                      // 0x10 means SW is not pressed
      GPIO_PORTF_DATA_R = 0x02;  // LED is pink
    }
    Delay();                     // wait 0.1 sec
    GPIO_PORTF_DATA_R = 0x04;    // LED is sky blue
    Delay();                     // wait 0.1 sec
  }
}

void PortF_Init(void){ volatile unsigned long delay;
  SYSCTL_RCGC2_R |= 0x00000020;     // 1) F clock
  delay = SYSCTL_RCGC2_R;           // delay   
  GPIO_PORTF_LOCK_R = 0x4C4F434B;   // 2) unlock PortF PF0  
  GPIO_PORTF_CR_R = 0x1F;           // allow changes to PF4-0       
  GPIO_PORTF_AMSEL_R = 0x00;        // 3) disable analog function
  GPIO_PORTF_PCTL_R = 0x00000000;   // 4) GPIO clear bit PCTL  
  GPIO_PORTF_DIR_R = 0x0E;          // 5) PF4,PF0 input, PF3,PF2,PF1 output   00001110
  GPIO_PORTF_AFSEL_R = 0x00;        // 6) no alternate function 
  GPIO_PORTF_PUR_R = 0x11;          // enable pullup resistors on PF4,PF0       00010001
  GPIO_PORTF_DEN_R = 0x1F;          // 7) enable digital pins PF4-PF0        00011111
}
// =============== End for 3 leds  ===============



// Color    LED(s) PortFB
// dark     ---    0
// red      R--    0x02
// blue     --B    0x04
// green    -G-    0x08
// yellow   RG-    0x0A
// sky blue -GB    0x0C
// white    RGB    0x0E
// pink     R-B    0x06

// Subroutine to wait 0.1 sec
// Inputs: None
// Outputs: None
// Notes: ...
void Delay(void){unsigned long volatile time;
  time = 727240*200/91;  // 0.1sec
  while(time){
		time--;
  }
}
